package com.cuitrip.business.home.travel;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.cuitrip.business.tripservice.ui.IconTextView;
import com.cuitrip.service.R;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragment;

import butterknife.ButterKnife;
import butterknife.Bind;

/**
 * Created by sheena on 15/11/5.
 */
public class TravelTripLanguageShowFragment extends CustomUiFragment{


    private String[] selectArray;
    private String[] selectedValues;

    @Bind(R.id.language_list)
    ListView languageList;

    public TravelTripLanguageShowFragment(){}

   /* @SuppressLint("ValidFragment")
    public TravelTripLanguageShowFragment(String[] selectedValues) {
        this.selectedValues=selectedValues;
    }*/

    public static TravelTripLanguageShowFragment newInstance(String[] selectedValues){

        TravelTripLanguageShowFragment languageShowFragment=new TravelTripLanguageShowFragment();
        Bundle bundle=new Bundle();
        bundle.putStringArray("selectedValues",selectedValues);
        languageShowFragment.setArguments(bundle);
        return languageShowFragment;
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {

        CustomUiConfig customUiConfig=new CustomUiConfig();
        customUiConfig.mTopbarLeftAreaName=getString(R.string.back_icon);
        customUiConfig.mTopbarMiddleAreaName=getString(R.string.filter_option_language);
        return customUiConfig;
    }

    @Override
    public void onTopbarLeftPress() {
        Pair<String, String> dataPair = saveMultiSelect();
        if(TextUtils.isEmpty(dataPair.first)){
            mDataSendBackListenr.dataSendBack("",getString(R.string.value_no_limit));
        }else {
            mDataSendBackListenr.dataSendBack(dataPair.first,dataPair.second);
        }
        getHostActivity().onBackPressed();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=super.onCreateView(inflater, container, savedInstanceState, R.layout.fragment_language_show);
        ButterKnife.bind(this, view);
        selectArray=getHostActivity().getResources().getStringArray(R.array.languages);
        this.selectedValues=getArguments().getStringArray("selectedValues");
        doMultiSelect();
        return view;

    }

    @Override
    public void initView() {

    }

    private void doMultiSelect() {
        languageList.setVisibility(View.VISIBLE);
        final LanguageAdapter adapter=new LanguageAdapter(selectArray,languageList);
        languageList.setAdapter(adapter);
        if(selectedValues!=null) {
            for (int i = 0; i < selectedValues.length; i++) {
                int index = -1;
                for (int j = 0; j < selectArray.length; j++) {
                    if (selectArray[j].equals(selectedValues[i])) {
                        index = j;
                        break;
                    }
                }
                if (index != -1) {
                    languageList.setItemChecked(index, true);
                }
            }
        }
        languageList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.notifyDataSetChanged();
            }
        });
    }

    private Pair<String, String> saveMultiSelect() {

        String language="";
        String languageCode = "";
        for (int i = 0; i < selectArray.length; i++) {
            if (languageList.isItemChecked(i)) {
                if (!TextUtils.isEmpty(language)){
                    language+="/";
                    languageCode += ",";
                }
                language+=selectArray[i];
                languageCode+=selectArray[i];
            }
        }
        return Pair.create(languageCode, language);

    }

    private static class LanguageAdapter extends BaseAdapter {

        private final String[] selectArray;
        private ListView listView;

        public LanguageAdapter(String[] selectArray, ListView listView) {
            this.selectArray = selectArray;
            this.listView = listView;
        }

        @Override
        public int getCount() {
            return selectArray.length;
        }

        @Override
        public String getItem(int position) {
            return selectArray[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_language, parent, false);
                holder = new ViewHolder();
                holder.language = (TextView) convertView.findViewById(R.id.tvLanguage);
                holder.tvState = (IconTextView) convertView.findViewById(R.id.tvState);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.language.setText(getItem(position));
            holder.tvState.setText(listView.isItemChecked(position) ? R.string.correct_icon : R.string.unselected_icon);
            holder.tvState.setTextColor(convertView.getResources().getColor(listView.isItemChecked(position) ? R.color.apple_7ecc1e : R.color.ganshi_ded8d7));
            return convertView;
        }

        private static final class ViewHolder {
            TextView language;
            IconTextView tvState;
        }
    }

    public interface DataSendBackListener{
        public void dataSendBack(String languageCode, String data);
    }

    private DataSendBackListener mDataSendBackListenr;

    public void setResultListener(DataSendBackListener listener){
        mDataSendBackListenr=listener;
    }


}
